﻿using System.Collections;
using System.Collections.Generic;

namespace GxFramework
{
    public abstract class CompositeWork : BrankWork
    {
        List<WorkObject> _children = new List<WorkObject>();
        override public int childCount { get { return _children.Count; } }
        override public void AddChild(WorkObject wo)
        {
            if (wo == this || wo.parent == this)//自己或孩子
                return;
            if (wo is CompositeWork)//祖先
            {
                if (IsAncestor(wo as CompositeWork))
                    return ;
            }
            _children.Add(wo);
            wo.SetParent(this);
        }
        public void AddChildAt(WorkObject wo,int index,bool setChildIndexWhenExist=true)
        {
            if (wo == this)//自己
                return;
            if (wo.parent == this)//孩子
            {
                if (setChildIndexWhenExist)
                    SetChildIndex(wo,index);
                return;
            }
            if (wo is CompositeWork)//祖先
            {
                if (IsAncestor(wo as CompositeWork))
                    return;
            }
            _children.Insert(index,wo);
            wo.SetParent(this);
        }
        public void SetChildIndex(WorkObject work, int index)
        {
 
        }
        public int GetChildIndex(WorkObject work)
        {
            if (work.parent != this)
                return -1;
            return _children.IndexOf(work);
        }
        override public WorkObject GetChildAt(int index)
        {
            return _children[index];
        }
        virtual internal protected void OnChildDone(WorkObject work)
        {
 
        }
        public override bool OnEvent(string eventName)
        {
            bool hasRegisterCurEvent = false;
            if (hasRegisterCurEvent)
            {
                bool bContinue = true;
                var subWork = this.GetCurrentWork();
                if (null != subWork)
                    bContinue = subWork.OnEvent(eventName);
                if (bContinue)
                    bContinue = base.OnEvent(eventName);
                return bContinue;
            }
            return true;
        }
    }
}
